package lvbudong.com.yanzhengma;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView imgYanzheng;
    private Button btn_change, btn_commit;
//    SharedPreferences preferences;
//    SharedPreferences.Editor editor;
    private EditText edit_yanzhengma;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgYanzheng = (ImageView) findViewById(R.id.imgYanzheng);
        btn_change = (Button) findViewById(R.id.btn_change);
        btn_commit = (Button) findViewById(R.id.btn_commit);
        edit_yanzhengma = (EditText) findViewById(R.id.ed_yanzhengma);
        imgYanzheng.setImageBitmap(CodeUtils.getInstance().createBitmap());
        btn_change.setOnClickListener(this);
        btn_commit.setOnClickListener(this);

//        preferences = getSharedPreferences("yanzhengma", Context.MODE_PRIVATE);
//        editor = preferences.edit();
//        String yanzhengma = CodeUtils.getInstance().getCode();
//        editor.putString("yanzhengma", yanzhengma);
//        editor.commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_change:
                //生成验证码并显示在图片上
                imgYanzheng.setImageBitmap(CodeUtils.getInstance().createBitmap());
//                editor.putString("yanzhengma", CodeUtils.getInstance().getCode());
//                editor.commit();
                edit_yanzhengma.setText(null);
                break;
            case R.id.btn_commit:
//                String yanzhengma = preferences.getString("yanzhengma", "");
                String yanzhengma = CodeUtils.getInstance().getCode();
                if (edit_yanzhengma.getText().toString().equals(yanzhengma)) {
                    Intent intent = new Intent(this, Lvbudong.class);
                    startActivity(intent);
                } else if (edit_yanzhengma.getText().toString() == null) {
                    Toast.makeText(this, "请输入验证码", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "验证码不正确，请重新输入", Toast.LENGTH_SHORT).show();
                }
                break;

        }
    }
}
